import {Injectable} from '@angular/core';
import {Storage} from '@ionic/storage';
import {StorageKey} from '../Constants';

@Injectable({
    providedIn: 'root'
})
export class StorageService {

    constructor(private storage: Storage) {}

    async write(key: string, value: any) {
        return await this.storage.set(key, value);
    }

    async read(key: string): Promise<any> {
        let info = JSON.parse(JSON.stringify(await this.storage.get(key)));
        if (typeof info === 'string') {
            try {
                info = JSON.parse(info);
            } catch (e) {
            }
        }
        return info;
    }

    async remove(key: string) {
        return await this.storage.remove(key);
    }

    async clear() {
        return await this.storage.clear();
    }

    /**
     * 退出登录调用,需要同步执行
     */
    logOut() {

        this.remove('alert1');
        this.remove('alert');

        this.remove(StorageKey.PERSON_INFO);
    }

}
